package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.KhoiKienThucM;
import net.sf.json.JSONObject;
import dao.KhoiKienThucD;

@WebServlet("/KhoiKienThucServlet")
public class KhoiKienThucServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private KhoiKienThucD dao;
       
    /**
     * @function constructor KhoiKienThucServlet
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public KhoiKienThucServlet() throws Exception {
        super();
        this.dao = new KhoiKienThucD();
    }
    
    /**
	 * Destruction of the servlet.
	 */
	public void destroy() {
		super.destroy();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		
		String flag = request.getParameter("flag");
		try {
			if (flag == null || "".equals(flag.trim())) {
				showKKT(request, response);
			}else if ("add".equals(flag.trim())) {
				String id_ctgd = request.getParameter("id_ctgd");
				
				String id_loai_khoi_kien_thuc1 = request.getParameter("id_loai_khoi_kien_thuc1");
				String ten1 = request.getParameter("ten1");
				String so_tc_bb1 = request.getParameter("so_tc_bb1");
				String so_tc_tc1 = request.getParameter("so_tc_tc1");
				String so_tc_dk1 = request.getParameter("so_tc_dk1");

				String id_loai_khoi_kien_thuc2 = request.getParameter("id_loai_khoi_kien_thuc2");
				String ten2 = request.getParameter("ten2");
				String so_tc_bb2 = request.getParameter("so_tc_bb2");
				String so_tc_tc2 = request.getParameter("so_tc_tc2");
				String so_tc_dk2 = request.getParameter("so_tc_dk2");
				
				String id_loai_khoi_kien_thuc3 = request.getParameter("id_loai_khoi_kien_thuc3");
				String ten3 = request.getParameter("ten3");
				String so_tc_bb3 = request.getParameter("so_tc_bb3");
				String so_tc_tc3 = request.getParameter("so_tc_tc3");
				String so_tc_dk3 = request.getParameter("so_tc_dk3");
				
				addKKT(ten1, Integer.parseInt(so_tc_bb1), Integer.parseInt(so_tc_tc1), Integer.parseInt(so_tc_dk1), Integer.parseInt(id_ctgd),Integer.parseInt(id_loai_khoi_kien_thuc1),request,response);
				addKKT(ten2, Integer.parseInt(so_tc_bb2), Integer.parseInt(so_tc_tc2), Integer.parseInt(so_tc_dk2), Integer.parseInt(id_ctgd),Integer.parseInt(id_loai_khoi_kien_thuc2),request,response);
				addKKT(ten3, Integer.parseInt(so_tc_bb3), Integer.parseInt(so_tc_tc3), Integer.parseInt(so_tc_dk3), Integer.parseInt(id_ctgd),Integer.parseInt(id_loai_khoi_kien_thuc3),request,response);
			}else if ("edit".equals(flag.trim())) {
				String id_khoi_kien_thuc = request.getParameter("id_khoi_kien_thuc");
				String ten = request.getParameter("ten");
				String so_tc_bb = request.getParameter("so_tc_bb");
				String so_tc_tc = request.getParameter("so_tc_tc");
				String so_tc_dk = request.getParameter("so_tc_dk");
				String id_ctgd = request.getParameter("id_ctgd");	
				String id_loai_khoi_kien_thuc = request.getParameter("id_loai_khoi_kien_thuc");
				editKKT(Integer.parseInt(id_khoi_kien_thuc), ten, Integer.parseInt(so_tc_bb), Integer.parseInt(so_tc_tc), Integer.parseInt(so_tc_dk), Integer.parseInt(id_ctgd),Integer.parseInt(id_loai_khoi_kien_thuc), request, response);
			}else if ("remove".equals(flag.trim())) {
				String[] id_khoi_kien_thuc = request.getParameter("id").split(",");
				for(int i=0; i<id_khoi_kien_thuc.length; i++){
					deleteKKT(Integer.parseInt(id_khoi_kien_thuc[i]), request, response);
				}	
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * @function show KhoiKienThuc
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void showKKT(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String pageCount = request.getParameter("page");
		String rowsCount = request.getParameter("rows");
		String search=request.getParameter("searchTen")==null?"":request.getParameter("searchTen");
		String id=request.getParameter("id")==null?"0":request.getParameter("id");
		
		int page = (pageCount == null || "".equals(pageCount)) ? 1 : Integer.valueOf(pageCount);
		int rows = (rowsCount == null || "".equals(rowsCount)) ? 10 : Integer.valueOf(rowsCount);
		
		int offSet = (page - 1) * rows;
		int allCount = dao.getAll().size();
		
		returnMap.put("total", allCount);
		returnMap.put("rows", dao.getAlls (Integer.parseInt(id), offSet, rows));
		if(search!=""){
			returnMap.put("rows", dao.getAlls (search, offSet, rows));
		}
		
		String returnString = JSONObject.fromObject(returnMap).toString();
		PrintWriter out = response.getWriter();
		out.println(returnString);
		out.flush();
		out.close();
	}
	
	/**
	 * @function add KhoiKienThuc
	 * @param ten
	 * @param so_tc_bb
	 * @param so_tc_tc
	 * @param so_tc_dk
	 * @param id_ctgd
	 * @param id_loai_khoi_kien_thuc
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void addKKT(String ten, int so_tc_bb, int so_tc_tc, int so_tc_dk, int id_ctgd,int id_loai_khoi_kien_thuc, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		KhoiKienThucM kkt = new KhoiKienThucM();
		kkt.setTen(ten);
		kkt.setSo_tc_bb(so_tc_bb);
		kkt.setSo_tc_tc(so_tc_tc);
		kkt.setSo_tc_dk(so_tc_dk);
		kkt.setId_ctgd(id_ctgd);
		kkt.setId_loai_khoi_kien_thuc(id_loai_khoi_kien_thuc);
		if(dao.Insert(kkt)==true){
			map.put("success", "true");
			map.put("msg", "Thêm thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi thêm dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
	
	/**
	 * @function edit KhoiKienThuc
	 * @param id_khoi_kien_thuc
	 * @param ten
	 * @param so_tc_bb
	 * @param so_tc_tc
	 * @param so_tc_dk
	 * @param id_ctgd
	 * @param id_loai_khoi_kien_thuc
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void editKKT(int id_khoi_kien_thuc, String ten, int so_tc_bb, int so_tc_tc, int so_tc_dk, int id_ctgd,int id_loai_khoi_kien_thuc,  HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		KhoiKienThucM kkt = new KhoiKienThucM();
		kkt.setId_khoi_kien_thuc(id_khoi_kien_thuc);
		kkt.setTen(ten);
		kkt.setSo_tc_bb(so_tc_bb);
		kkt.setSo_tc_tc(so_tc_tc);
		kkt.setSo_tc_dk(so_tc_dk);
		kkt.setId_ctgd(id_ctgd);
		kkt.setId_loai_khoi_kien_thuc(id_loai_khoi_kien_thuc);
		if(dao.Update(kkt)==true){
			map.put("success", "true");
			map.put("msg", "Sửa thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi sửa dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
	
	/**
	 * @function delete KhoiKienThuc
	 * @param id_khoi_kien_thuc
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void deleteKKT(int id_khoi_kien_thuc, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if(dao.Delete(id_khoi_kien_thuc)==true){
			map.put("success", "true");
			map.put("msg", "Xóa thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi xóa dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
}